package portfolio;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class WebTasks {
	
	DatabaseParser catalog;
	
	public WebTasks(DatabaseParser book){
		catalog = book;
	}
	
	public void sortByDate(boolean accending){
		
		// Sort by Date
		List<Page> allPages = catalog.getAllPages();
		int size = allPages.size();
		for(int i = 0; i < size; i++){
			int smallest = i;
			Date small = allPages.get(smallest).getDate();
			
			for(int j = i; j < size; j++){				
				Date challenge = allPages.get(j).getDate();
				
				if((small == null && challenge != null) || (challenge != null && small.after(challenge))){
					smallest = j;
					small = allPages.get(smallest).getDate(); 
				}
			}
			
			Collections.swap(allPages, i, smallest);
		}
		
		// Print sorted database to the DatabaseParser
		/*for(int i = 0; i < allPages.size(); i++){
			Page page = allPages.get(i);
			String output = String.format("%1$tm/%1$te/%1$tY", page.getDate());
			System.out.println(output);
		}*/

		catalog.replaceDatabase(allPages);
	}
	
	public void sortByTitle(boolean accending){
		
	}
		
}
